package com.ssyc.student.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StDepotPropAdapter;
import com.ssyc.student.bean.DepotInfo;

import java.util.List;


/**
 * 学生端--补给站--道具Fragment
 */

public class StudentDepotPropFragment extends LazyBaseFragment {

    //列表控件
    private RecyclerView rv;

    //列表的数据集合
    private List<DepotInfo.DataBean.Type3Bean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private StDepotPropAdapter adapter;

    //单例模式
    private static volatile StudentDepotPropFragment studentDepotPropFragment;

    public static StudentDepotPropFragment newInstance(String data) {
        if (studentDepotPropFragment == null) {
            synchronized (StudentDepotPropFragment.class) {
                if (studentDepotPropFragment == null) {
                    studentDepotPropFragment = new StudentDepotPropFragment();
                    //通过Bundle传递页面需要展示的集合数据的json
                    Bundle bundle = new Bundle();
                    bundle.putString("data", data);
                    studentDepotPropFragment.setArguments(bundle);
                }
            }
        }
        return studentDepotPropFragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_depot_prop;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        showContent();

        //初始化视图
        initView(view);

        //获取通过Activity传递过过来的页面数据
        initData();

        //设置rv
        initRv();
    }


    /**
     * 初始化View
     */
    private void initView(View view) {
        rv = view.findViewById(R.id.rv);
    }


    /**
     * 获取页面数据的方法
     */
    private void initData() {
        String data = getArguments().getString("data");
        if (!TextUtils.isEmpty(data)) {
            try {
                oldDatas = GsonUtil.jsonToList(data, DepotInfo.DataBean.Type3Bean[].class);
            } catch (Exception e) {
                Log.i("test", e.toString());
            }
        }
    }

    /**
     * 设置列表
     */
    private void initRv() {
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view, null);
        adapter = new StDepotPropAdapter(getActivity(), getContext(), R.layout.student_rv_depot_prop, oldDatas);
        //设置横向的布局管理器,横向布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        rv.setLayoutManager(manager);
        adapter.setEmptyView(emptyView);
        rv.setAdapter(adapter);
    }

    /**
     * EventBus刷新数据
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && busInfo.useMoney != 0) {
            adapter.notifyDataSetChanged();
        }
    }


}
